<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var maxOperations = function (nums, k) {
      nums = nums.sort(function (a, b) { return a - b });
      var l = 0, r = nums.length - 1, ant = 0;
      while (l < r) {
        // console.log('@@@ 15', l)
        // console.log('@@@ 16', r)
        var sum = nums[l] + nums[r];
        console.log('@@@ 20', nums[l])
        console.log('@@@ 19', nums[r])
        console.log('@@@ 21', sum)
        if (sum == k) {
          ant++;
          l++;
          r--;
        } else if (k > sum) {
          l++;
        } else {
          r--;
        }
      }
      return ant
    };
    console.log(maxOperations(nums = [1, 2, 3, 4], k = 5))
  </script>
</body>

</html>